from celery import shared_task

from utils.jenkins_api import JenkinsSdk
from django.core.cache import cache
from celery.result import AsyncResult


def on_success_callback(task_fun, result, task_id, *args, **kwargs):
    cache.set('jenkins_data', result, timeout=600)
    backend_result = AsyncResult(id=task_id)
    backend_result.forget()


@shared_task(on_success=on_success_callback)
def get_jenkins_list():
    jenkins = JenkinsSdk()
    job_list = jenkins.get_job_list()['data']
    return job_list
#
#
# @shared_task
# def add(x, y):
#     return x + y
